/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAO.FeedbackDAO;
import hibernate.entity.Account;
import hibernate.entity.Feedback;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tùng
 */
public class FeedbackController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        String url = "FeedbackController?btAction=ShowFeedback";
        try {
            String action = request.getParameter("btAction");
            if (action.equals("sendFeedback")) {
                Account acc = (Account) session.getAttribute("ACCOUNT");
                if (acc != null) {
                    int accountID = acc.getAccountID();
                    String feedbackBody = request.getParameter("txtFeedback");
                    FeedbackDAO fbDAO = new FeedbackDAO();
                    boolean check = fbDAO.createFeedback(accountID, feedbackBody);
                    if (check) {
                        session.setAttribute("CREATEFEEDSUC", "You sent feedback successfully!!!");
                    } else {
                        session.setAttribute("CREATEFEEDERR", "You sent feedback failed!!!");
                    }
                    response.sendRedirect(url);
                } else if (acc == null) {
                    response.sendRedirect(Common.Global.loginpage);
                }
            } else if (action.equals("ShowFeedback")) {
                FeedbackDAO fbDAO = new FeedbackDAO();
                List<Feedback> result = fbDAO.loadFeedback();
                session.setAttribute("ALLFEEDBACK", result);
                response.sendRedirect(Common.Global.feedbackpage);
            } else if (action.equals("deleteFeedback")) {
                int feedbackID = Integer.parseInt(request.getParameter("feedbackID"));
                FeedbackDAO fbDAO = new FeedbackDAO();
                boolean check = fbDAO.deleteFeedback(feedbackID);
                if (check) {
                    session.setAttribute("DELETEFEEDSUC", "You deleted feedback successfully!!!");
                } else {
                    session.setAttribute("DELETEFEEDERR", "You deleted feedback failed!!!");
                }
                response.sendRedirect(url);
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
